﻿using Qy.Core;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Qy.Scenario;

[AppService]
public class DictDataService : QyRepository<DictData>
{
    private readonly ApplicationContext _applicationContext;
    private readonly string _cacheKeyAll;
    public DictDataService(ApplicationContext applicationContext) : base(applicationContext)
    {
        _applicationContext = applicationContext;
        _cacheKeyAll = GetEntityFullName();
    }

    public async Task<PagedInfo<ViewDictData>> GetPageListAsync(QueryDictData parm)
    {
        var exp = Expressionable.Create<DictData>();
        exp = exp.AndIF(parm.Cid > 0, data => data.Cid == parm.Cid);
        return await Queryable()
            .LeftJoin<DictCat>((data, cat) => data.Cid == cat.Id)
            .Where(exp.ToExpression())
            .WhereIF(!string.IsNullOrEmpty(parm.Value), (data, cat) => data.Value.Contains(parm.Value))
            .OrderBy((data) => data.Id, OrderByType.Desc)
            .Select((data, cat) => new ViewDictData()
            {
                CatName = cat.CatName,
                CatCode = cat.CatCode,
                Id = data.Id.SelectAll()
            })
            .ToPageAsync(parm);
    }

    public async Task<ViewDictData> GetEditAsync(int id)
    {
        return await Queryable()
                   .LeftJoin<DictCat>((data, cat) => data.Cid == cat.Id)
                   .Select((data, cat) => new ViewDictData()
                   {
                       CatName = cat.CatName.SelectAll(),
                       Id = data.Id.SelectAll()
                   })
                   .FirstAsync(data => data.Id == id);
    }

    public async Task<bool> InsertDelCache(DictData dictData)
    {
        bool res = await InsertAsync(dictData);
        if (res) await _applicationContext.ICacheManager.RemoveAsync(_cacheKeyAll);
        return res;
    }

    public async Task<int> UpdateDelCache(DictData editDictData)
    {
        DictData dictData = await GetIdAsync(editDictData.Id);
        if (dictData == null)
            return 0;
        dictData.Cid = editDictData.Cid;
        dictData.Key = editDictData.Key;
        dictData.Value = editDictData.Value;
        dictData.Remark = editDictData.Remark;
        dictData.Status = editDictData.Status;
        await _applicationContext.ICacheManager.RemoveAsync(_cacheKeyAll);
        return await UpdateAsync(dictData);
    }

    public async Task<List<ListViewDictData>> GetListAllAsync()
    {
        List<ListViewDictData> dictDatas = _applicationContext.ICacheManager.Get<List<ListViewDictData>>(_cacheKeyAll);
        if (dictDatas == null)
        {
            dictDatas = await Queryable()
                .LeftJoin<DictCat>((data, cat) => data.Cid == cat.Id)
                .Where((data, cat) => cat.Status == true && data.Status == true)
                .OrderBy((data) => data.Id, OrderByType.Desc)
                .Select((data, cat) => new ListViewDictData()
                {
                    Key = data.Key,
                    Value = data.Value,
                    CatCode = cat.CatCode
                })
                .ToListAsync();
            _applicationContext.ICacheManager.Set(_cacheKeyAll, dictDatas, TimeSpan.FromDays(30));
        }
        return dictDatas;
    }
}
